package com.iplant.aps.serviceimpl.dao.aps;

import com.iplant.aps.service.mesenum.BFCMessageType;
import com.iplant.aps.service.mesenum.BPMEventModule;
import com.iplant.aps.service.mesenum.MESDBSource;
import com.iplant.aps.service.mesenum.MESException;
import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSTaskStepCancelLogBPM;
import com.iplant.aps.service.po.bfc.BFCMessage;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.bpm.BPMTaskBase;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.CoreServiceImpl;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import com.iplant.aps.serviceimpl.dao.TaskBaseDAO;
import com.iplant.aps.serviceimpl.dao.oms.OMSOrderDAO;
import com.iplant.aps.serviceimpl.utils.aps.APSConstans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;

public class APSTaskStepCancelLogBPMDAO extends BaseDAO implements TaskBaseDAO {

    private static final Logger logger = LoggerFactory.getLogger(APSTaskStepCancelLogBPMDAO.class);

    private static APSTaskStepCancelLogBPMDAO Instance = null;

    /**
     * 添加或修改
     */
    public APSTaskStepCancelLogBPM Update(BMSEmployee wLoginUser, APSTaskStepCancelLogBPM wAPSTaskStepCancelLogBPM,
                                          OutResult<Integer> wErrorCode) {
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wAPSTaskStepCancelLogBPM;
            }

            if (wAPSTaskStepCancelLogBPM == null)
                return null;

            String wSQL = "";
            if (wAPSTaskStepCancelLogBPM.getID() <= 0) {
                wSQL = MessageFormat.format(
                        "INSERT INTO {0}.aps_taskstepcancellogbpm(Code,FlowType,FlowID,UpFlowID,FollowerID,Status," +
                                "StatusText,CreateTime,SubmitTime,OrderID,PartNo,PartID,StepIDs,CancelType," +
                                "DescribeInfo) VALUES(:Code,:FlowType,:FlowID,:UpFlowID,:FollowerID,:Status," +
                                ":StatusText,:CreateTime,:SubmitTime,:OrderID,:PartNo,:PartID,:StepIDs,:CancelType," +
                                ":DescribeInfo);",
                        wInstance.Result);
            } else {
                wSQL = MessageFormat.format(
                        "UPDATE {0}.aps_taskstepcancellogbpm SET Code = :Code,FlowType = :FlowType,FlowID = :FlowID," +
                                "UpFlowID = :UpFlowID,FollowerID = :FollowerID,Status = :Status,StatusText = " +
                                ":StatusText,CreateTime = :CreateTime,SubmitTime = :SubmitTime,OrderID = :OrderID," +
                                "PartNo = :PartNo,PartID = :PartID,StepIDs = :StepIDs,CancelType = :CancelType," +
                                "DescribeInfo = :DescribeInfo WHERE ID = :ID;",
                        wInstance.Result);
            }

            wSQL = this.DMLChange(wSQL);

            Map<String, Object> wParamMap = new HashMap<>();

            wParamMap.put("ID", wAPSTaskStepCancelLogBPM.ID);
            wParamMap.put("Code", wAPSTaskStepCancelLogBPM.Code);
            wParamMap.put("FlowType", wAPSTaskStepCancelLogBPM.FlowType);
            wParamMap.put("FlowID", wAPSTaskStepCancelLogBPM.FlowID);
            wParamMap.put("UpFlowID", wAPSTaskStepCancelLogBPM.UpFlowID);
            wParamMap.put("FollowerID", StringUtils.Join(",", wAPSTaskStepCancelLogBPM.FollowerID));
            wParamMap.put("Status", wAPSTaskStepCancelLogBPM.Status);
            wParamMap.put("StatusText", wAPSTaskStepCancelLogBPM.StatusText);
            wParamMap.put("CreateTime", wAPSTaskStepCancelLogBPM.CreateTime);
            wParamMap.put("SubmitTime", wAPSTaskStepCancelLogBPM.SubmitTime);
            wParamMap.put("OrderID", wAPSTaskStepCancelLogBPM.OrderID);
            wParamMap.put("PartNo", wAPSTaskStepCancelLogBPM.PartNo);
            wParamMap.put("PartID", wAPSTaskStepCancelLogBPM.PartID);
            wParamMap.put("StepIDs", wAPSTaskStepCancelLogBPM.StepIDs);
            wParamMap.put("CancelType", wAPSTaskStepCancelLogBPM.CancelType);
            wParamMap.put("DescribeInfo", wAPSTaskStepCancelLogBPM.DescribeInfo);

            KeyHolder keyHolder = new GeneratedKeyHolder();
            SqlParameterSource wSqlParameterSource = new MapSqlParameterSource(wParamMap);

            shrisJdbcTemplate.update(wSQL, wSqlParameterSource, keyHolder);

            if (wAPSTaskStepCancelLogBPM.getID() <= 0) {
                wAPSTaskStepCancelLogBPM.ID = Objects.requireNonNull(keyHolder.getKey()).intValue();
            }
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wAPSTaskStepCancelLogBPM;
    }

    /**
     * 删除集合
     */
    public ServiceResult<Integer> DeleteList(BMSEmployee wLoginUser, List<APSTaskStepCancelLogBPM> wList,
                                             OutResult<Integer> wErrorCode) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            if (wList == null || wList.size() <= 0)
                return wResult;

            List<String> wIDList = new ArrayList<>();
            for (APSTaskStepCancelLogBPM wItem : wList) {
                wIDList.add(String.valueOf(wItem.ID));
            }
            String wSql = MessageFormat.format("delete from {1}.aps_taskstepcancellogbpm WHERE ID IN({0}) ;",
                    String.join(",", wIDList), wInstance.Result);
            this.ExecuteSqlTransaction(wSql);
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 查单条
     */
    public APSTaskStepCancelLogBPM SelectByID(BMSEmployee wLoginUser, int wID, OutResult<Integer> wErrorCode) {
        APSTaskStepCancelLogBPM wResult = new APSTaskStepCancelLogBPM();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            List<APSTaskStepCancelLogBPM> wList = this.SelectList(wLoginUser, wID, "", -1, -1, -1, null, null, null, -1,
                    wErrorCode);
            if (wList == null || wList.size() != 1)
                return wResult;
            wResult = wList.get(0);
        } catch (Exception e) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(e.toString());
        }
        return wResult;
    }

    /**
     * 条件查询集合
     */
    public List<APSTaskStepCancelLogBPM> SelectList(BMSEmployee wLoginUser, int wID, String wCode, int wUpFlowID,
                                                    int wOrderID, int wPartID, Calendar wStartTime, Calendar wEndTime
            , List<Integer> wStateIDList,
                                                    int wCancelType, OutResult<Integer> wErrorCode) {
        List<APSTaskStepCancelLogBPM> wResultList = new ArrayList<>();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser.getCompanyID(), MESDBSource.Basic,
                    wLoginUser.getID(), 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResultList;
            }

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);
            if (wStartTime == null) {
                wStartTime = wBaseTime;
            }
            if (wEndTime == null) {
                wEndTime = wBaseTime;
            }
            if (wStartTime.compareTo(wEndTime) > 0) {
                return wResultList;
            }

            if (wStateIDList == null) {
                wStateIDList = new ArrayList<>();
            }

            String wSQL = MessageFormat.format("SELECT * FROM {0}.aps_taskstepcancellogbpm WHERE  1=1  "
                            + "and ( :wID <= 0 or :wID = ID ) " + "and ( :wCode is null or :wCode = '''' or :wCode = " +
                            "Code ) "
                            + "and ( :wUpFlowID <= 0 or :wUpFlowID = UpFlowID ) "
                            + "and ( :wCancelType <= 0 or :wCancelType = CancelType ) "
                            + "and ( :wStartTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wStartTime <=  " +
                            "SubmitTime ) "
                            + "and ( :wEndTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :wEndTime >=  " +
                            "CreateTime ) "
                            + "and ( :wStatus is null or :wStatus = '''' or Status in ({1})) "
                            + "and ( :wOrderID <= 0 or :wOrderID = OrderID ) " + "and ( :wPartID <= 0 or :wPartID = " +
                            "PartID );",
                    wInstance.Result, wStateIDList.size() > 0 ? StringUtils.Join(",", wStateIDList) : "0");

            Map<String, Object> wParamMap = new HashMap<>();

            wParamMap.put("wID", wID);
            wParamMap.put("wCode", wCode);
            wParamMap.put("wUpFlowID", wUpFlowID);
            wParamMap.put("wOrderID", wOrderID);
            wParamMap.put("wPartID", wPartID);
            wParamMap.put("wStartTime", wStartTime);
            wParamMap.put("wEndTime", wEndTime);
            wParamMap.put("wCancelType", wCancelType);
            wParamMap.put("wStatus", StringUtils.Join(",", wStateIDList));

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = shrisJdbcTemplate.queryForList(wSQL, wParamMap);

            SetValue(wResultList, wQueryResult);
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResultList;
    }

    private void SetValue(List<APSTaskStepCancelLogBPM> wResultList, List<Map<String, Object>> wQueryResult) {
        try {
            for (Map<String, Object> wReader : wQueryResult) {
                APSTaskStepCancelLogBPM wItem = new APSTaskStepCancelLogBPM();

                wItem.ID = StringUtils.parseInt(wReader.get("ID"));
                wItem.Code = StringUtils.parseString(wReader.get("Code"));
                wItem.FlowType = StringUtils.parseInt(wReader.get("FlowType"));
                wItem.FlowID = StringUtils.parseInt(wReader.get("FlowID"));
                wItem.UpFlowID = StringUtils.parseInt(wReader.get("UpFlowID"));
                wItem.FollowerID = StringUtils
                        .parseIntList(StringUtils.parseString(wReader.get("FollowerID")).split(","));
                wItem.Status = StringUtils.parseInt(wReader.get("Status"));
                wItem.StatusText = StringUtils.parseString(wReader.get("StatusText"));
                wItem.CreateTime = StringUtils.parseCalendar(wReader.get("CreateTime"));
                wItem.SubmitTime = StringUtils.parseCalendar(wReader.get("SubmitTime"));
                wItem.OrderID = StringUtils.parseInt(wReader.get("OrderID"));
                wItem.PartNo = StringUtils.parseString(wReader.get("PartNo"));
                wItem.PartID = StringUtils.parseInt(wReader.get("PartID"));
                wItem.StepIDs = StringUtils.parseString(wReader.get("StepIDs"));
                wItem.CancelType = StringUtils.parseInt(wReader.get("CancelType"));
                wItem.DescribeInfo = StringUtils.parseString(wReader.get("DescribeInfo"));

                if (wItem.OrderID > 0) {
                    OutResult<Integer> wErrorCode = new OutResult<>(0);
                    OMSOrder wOrder = OMSOrderDAO.getInstance().SelectByID(BaseDAO.SysAdmin, wItem.OrderID, wErrorCode);
                    if (wOrder != null && wOrder.ID > 0) {
                        wItem.ProductID = wOrder.ProductID;
                        wItem.ProductNo = wOrder.ProductNo;
                        wItem.CustomerID = wOrder.CustomerID;
                        wItem.Customer = wOrder.Customer;
                        wItem.LineID = wOrder.LineID;
                        wItem.LineName = wOrder.LineName;
                        wItem.PartNo = wOrder.PartNo;
                    }
                }

                wItem.UpFlowName = APSConstans.GetBMSEmployeeName(wItem.UpFlowID);
                wItem.PartName = APSConstans.GetFPCPartName(wItem.PartID);
                wItem.StepNames = GetStepNames(wItem.StepIDs);

                wResultList.add(wItem);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    /**
     * 获取工序名称
     */
    private String GetStepNames(String stepIDs) {
        String wResult = "";
        try {
            if (StringUtils.isEmpty(stepIDs)) {
                return wResult;
            }

            String[] wStrs = stepIDs.split(",");
            List<String> wNames = new ArrayList<>();
            for (String wStr : wStrs) {
                int wStepID = StringUtils.parseInt(wStr);
                if (wStepID <= 0) {
                    continue;
                }
                String wName = APSConstans.GetFPCPartPointName(wStepID);
                if (StringUtils.isEmpty(wName)) {
                    continue;
                }
                wNames.add(wName);
            }
            wResult = StringUtils.Join(",", wNames);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    /**
     * 获取最新的编码
     */
    public String GetNewCode(BMSEmployee wLoginUser, OutResult<Integer> wErrorCode) {
        String wResult = "";
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser, MESDBSource.Basic, 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            // 本月时间
            int wYear = Calendar.getInstance().get(Calendar.YEAR);
            int wMonth = Calendar.getInstance().get(Calendar.MONTH);
            Calendar wSTime = Calendar.getInstance();
            wSTime.set(wYear, wMonth, 1, 0, 0, 0);
            Calendar wETime = Calendar.getInstance();
            wETime.set(wYear, wMonth + 1, 1, 23, 59, 59);
            wETime.add(Calendar.DATE, -1);

            String wSQL = StringUtils.Format(
                    "select count(*)+1 as Number from {0}.aps_taskstepcancellogbpm where CreateTime > :wSTime and " +
                            "CreateTime < :wETime;",
                    wInstance.Result);

            Map<String, Object> wParamMap = new HashMap<>();
            wParamMap.put("wSTime", wSTime);
            wParamMap.put("wETime", wETime);

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = shrisJdbcTemplate.queryForList(wSQL, wParamMap);

            int wNumber = 0;
            for (Map<String, Object> wReader : wQueryResult) {
                if (wReader.containsKey("Number")) {
                    wNumber = StringUtils.parseInt(wReader.get("Number"));
                    break;
                }
            }

            wResult = StringUtils.Format("SC{0}{1}{2}", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)),
                    String.format("%02d", Calendar.getInstance().get(Calendar.MONTH) + 1),
                    String.format("%04d", wNumber));
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    private APSTaskStepCancelLogBPMDAO() {
        super();
    }

    public static APSTaskStepCancelLogBPMDAO getInstance() {
        if (Instance == null)
            Instance = new APSTaskStepCancelLogBPMDAO();
        return Instance;
    }

    public List<APSTaskStepCancelLogBPM> SelectList(BMSEmployee wLoginUser, List<Integer> wTaskIDList,
                                                    Calendar wStartTime, Calendar wEndTime,
                                                    OutResult<Integer> wErrorCode) {
        List<APSTaskStepCancelLogBPM> wResultList = new ArrayList<>();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser, MESDBSource.Basic, 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResultList;
            }

            if (wTaskIDList == null || wTaskIDList.size() <= 0) {
                return wResultList;
            }

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2000, 0, 1, 0, 0, 0);
            if (wStartTime == null) {
                wStartTime = wBaseTime;
            }
            if (wEndTime == null) {
                wEndTime = wBaseTime;
            }
            if (wStartTime.compareTo(wEndTime) > 0) {
                return wResultList;
            }

            String wSQL = StringUtils.Format("SELECT * FROM {0}.aps_taskstepcancellogbpm WHERE  1=1  "
                            + "and ( :wStartTime <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wStartTime <= " +
                            "SubmitTime) "
                            + "and ( :wEndTime <= str_to_date(''2010-01-01'', ''%Y-%m-%d'') or :wEndTime >= " +
                            "CreateTime) "
                            + "and ( :wIDs is null or :wIDs = '''' or ID in ({1}));", wInstance.Result,
                    wTaskIDList.size() > 0 ? StringUtils.Join(",", wTaskIDList) : "0");

            Map<String, Object> wParamMap = new HashMap<>();

            wParamMap.put("wIDs", StringUtils.Join(",", wTaskIDList));
            wParamMap.put("wStartTime", wStartTime);
            wParamMap.put("wEndTime", wEndTime);

            wSQL = this.DMLChange(wSQL);

            List<Map<String, Object>> wQueryResult = shrisJdbcTemplate.queryForList(wSQL, wParamMap);

            SetValue(wResultList, wQueryResult);
        } catch (Exception ex) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(ex.toString());
        }
        return wResultList;
    }

    @Override
    public List<BPMTaskBase> BPM_GetUndoTaskList(BMSEmployee wLoginUser, int wResponsorID,
                                                 OutResult<Integer> wErrorCode) {
        List<APSTaskStepCancelLogBPM> wResult = new ArrayList<>();
        try {
            // 获取所有任务消息
            List<BFCMessage> wMessageList = CoreServiceImpl.getInstance()
                    .BFC_GetMessageList(wLoginUser, wLoginUser.getID(), BPMEventModule.TaskStepCancel.getValue(), -1,
                            BFCMessageType.Task.getValue(), 0, -1, null, null)
                    .List(BFCMessage.class);
            wMessageList.addAll(CoreServiceImpl.getInstance().BFC_GetMessageList(wLoginUser, wLoginUser.getID(),
                            BPMEventModule.TaskStepCancel.getValue(), -1, BFCMessageType.Task.getValue(), 1, -1, null
                            , null)
                    .List(BFCMessage.class));
            wMessageList.addAll(CoreServiceImpl.getInstance().BFC_GetMessageList(wLoginUser, wLoginUser.getID(),
                            BPMEventModule.TaskStepCancel.getValue(), -1, BFCMessageType.Task.getValue(), 2, -1, null
                            , null)
                    .List(BFCMessage.class));

            List<Integer> wTaskIDList = wMessageList.stream().map(p -> (int) p.MessageID).distinct()
                    .collect(Collectors.toList());

            // 所有未完成的任务
            Map<Integer, APSTaskStepCancelLogBPM> wTaskMap = new HashMap<>();
            if (wTaskIDList.size() > 0) {
                List<APSTaskStepCancelLogBPM> wMTCTaskListTemp = this.SelectList(wLoginUser, wTaskIDList, null, null,
                        wErrorCode);

                wTaskMap = wMTCTaskListTemp.stream().collect(Collectors.toMap(p -> p.ID, p -> p, (o1, o2) -> o1));

            }
            APSTaskStepCancelLogBPM wTaskTemp = null;
            for (BFCMessage wBFCMessage : wMessageList) {
                if (!wTaskMap.containsKey((int) wBFCMessage.getMessageID()))
                    continue;

                wTaskTemp = CloneTool.Clone(wTaskMap.get((int) wBFCMessage.getMessageID()),
                        APSTaskStepCancelLogBPM.class);
                wTaskTemp.StepID = wBFCMessage.getStepID();
                wResult.add(wTaskTemp);
            }

            wResult.sort(Comparator.comparing(APSTaskStepCancelLogBPM::getSubmitTime).reversed());
            // 剔除任务状态为0的任务（废弃任务）
            if (wResult.size() > 0) {
                wResult = wResult.stream().filter(p -> p.Status != 0).collect(Collectors.toList());
            }
        } catch (Exception e) {
            wErrorCode.set(MESException.Exception.getValue());
            logger.error(e.toString());
        }
        return new ArrayList<>(wResult);
    }

    @Override
    public List<BPMTaskBase> BPM_GetDoneTaskList(BMSEmployee wLoginUser, int wResponsorID, Calendar wStartTime,
                                                 Calendar wEndTime, OutResult<Integer> wErrorCode) {
        List<APSTaskStepCancelLogBPM> wResult = new ArrayList<>();
        wErrorCode.set(0);
        try {
            List<APSTaskStepCancelLogBPM> wTaskList = new ArrayList<>();
            // 获取所有任务消息
            List<BFCMessage> wMessageList = CoreServiceImpl.getInstance()
                    .BFC_GetMessageList(wLoginUser, wLoginUser.getID(), BPMEventModule.TaskStepCancel.getValue(), -1,
                            BFCMessageType.Task.getValue(), 3, -1, wStartTime, wEndTime)
                    .List(BFCMessage.class);
            wMessageList.addAll(CoreServiceImpl.getInstance()
                    .BFC_GetMessageList(wLoginUser, wLoginUser.getID(), BPMEventModule.TaskStepCancel.getValue(), -1,
                            BFCMessageType.Task.getValue(), 4, -1, wStartTime, wEndTime)
                    .List(BFCMessage.class));

            List<Integer> wTaskIDList = wMessageList.stream().map(p -> (int) p.MessageID).distinct()
                    .collect(Collectors.toList());

            wTaskList = this.SelectList(wLoginUser, wTaskIDList, wStartTime, wEndTime, wErrorCode);

            wTaskList.sort(Comparator.comparing(APSTaskStepCancelLogBPM::getSubmitTime).reversed());

            wResult = wTaskList;
            // 剔除任务状态为0的任务（废弃任务）
            if (wResult.size() > 0) {
                wResult = wResult.stream().filter(p -> p.Status != 0).collect(Collectors.toList());
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return new ArrayList<>(wResult);
    }

    @Override
    public List<BPMTaskBase> BPM_GetSendTaskList(BMSEmployee wLoginUser, int wResponsorID, Calendar wStartTime,
                                                 Calendar wEndTime, OutResult<Integer> wErrorCode) {
        List<APSTaskStepCancelLogBPM> wResult = new ArrayList<>();
        try {
            wResult = this.SelectList(wLoginUser, -1, "", wResponsorID, -1, -1, wStartTime, wEndTime, null, -1,
                    wErrorCode);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return new ArrayList<>(wResult);
    }

    @Override
    public BPMTaskBase BPM_UpdateTask(BMSEmployee wLoginUser, BPMTaskBase wTask, OutResult<Integer> wErrorCode) {
        BPMTaskBase wResult = new BPMTaskBase();
        try {
            wResult = this.Update(wLoginUser, (APSTaskStepCancelLogBPM) wTask, wErrorCode);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public BPMTaskBase BPM_GetTaskInfo(BMSEmployee wLoginUser, int wTaskID, String wCode,
                                       OutResult<Integer> wErrorCode) {
        BPMTaskBase wResult = new BPMTaskBase();
        try {
            ServiceResult<String> wInstance = this.GetDataBaseName(wLoginUser, MESDBSource.Basic, 0);
            wErrorCode.set(wInstance.ErrorCode);
            if (wErrorCode.Result != 0) {
                return wResult;
            }

            List<APSTaskStepCancelLogBPM> wList = this.SelectList(wLoginUser, wTaskID, wCode, -1, -1, -1, null, null,
                    null, -1, wErrorCode);
            if (wList == null || wList.size() != 1)
                return wResult;
            wResult = wList.get(0);
        } catch (Exception e) {
            wErrorCode.set(MESException.DBSQL.getValue());
            logger.error(e.toString());
        }
        return wResult;
    }
}
